package com.sl.au.service.impl;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.sl.au.entity.BussinessAccusation;
import com.sl.au.entity.Option;
import com.sl.au.entity.User;
import com.sl.au.repository.BussinessAccusationRepository;
import com.sl.au.service.BussinessAccusationService;
import com.sl.au.service.OptionService;

@Service
@Transactional
public class BussinessAccusationServiceImpl extends BaseServiceImpl implements BussinessAccusationService {

	@Autowired
	private BussinessAccusationRepository bussinessAccusationRepository;
	@Autowired
	OptionService optionService;

	@Override
	public List<BussinessAccusation> getPage(int start, int length, String key, String orderName, String dir) {
		Page<BussinessAccusation> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "accusationDate");
		pages = bussinessAccusationRepository.findAll(pageable);
		return pages.getContent();
	}

	@Override
	public BussinessAccusationRepository getRepository() {
		return this.bussinessAccusationRepository;
	}

	@Override
	public int getCount(String key) {
		if (!StringUtils.hasText(key))
			return bussinessAccusationRepository.findAll().size();
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "accusationDate");
		return bussinessAccusationRepository.getName(key, pageable).getContent().size();
	}

	@Override
	public List<BussinessAccusation> getAccusations(int start, int length, String key, String orderName, String dir) {
		Page<BussinessAccusation> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "accusationDate");
		pages = bussinessAccusationRepository.getName(key, pageable);
		return pages.getContent();
	}

	@Override
	public void deleteById(String id) {
		bussinessAccusationRepository.delete(bussinessAccusationRepository.findOne(id));
	}

	@Override
	public int receiveThing(String id, User user) {
		int code = -1;
		try {
			BussinessAccusation bussinessAccusation = this.getRepository().findOne(id);
			bussinessAccusation.setrUser(user);
			List<Option> option = optionService.getRepository().findByGroupAndTitle("投诉建议状态", "已受理");
			if (option.size() == 0) {
				code = -1;
			} else {
				bussinessAccusation.setInfoState(option.get(0));
				this.getRepository().save(bussinessAccusation);
				code = 1;
			}

		} catch (Exception ex) {
			code = -1;
		}
		return code;
	}

	@Override
	public int finishThing(String id, User user, String memo, String resultTypeId) {
		int code = -1;
		try {
			BussinessAccusation bussinessAccusation = this.getRepository().findOne(id);
			bussinessAccusation.setrUser(user);
			List<Option> option = optionService.getRepository().findByGroupAndTitle("投诉建议状态", "受理完成");
			if (option.size() == 0) {
				code = -1;
			} else {
				bussinessAccusation.setInfoState(option.get(0));
				bussinessAccusation.setResultType(this.optionService.getRepository().findOne(resultTypeId));
				bussinessAccusation.setMemo(memo);
				this.getRepository().save(bussinessAccusation);
				code = 1;
			}

		} catch (Exception ex) {
			code = -1;
		}
		return code;

	}
}
